﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Unity
{
    public class ProbabilitySelection
    {
        private List<ProbabilityObject> probabilityList;

        public List<ProbabilityObject> ProbabilityList
        {
            set
            {
                probabilityList = value;
            }
            get
            {
                return probabilityList;
            }
        }

        public ProbabilitySelection()
        {
            probabilityList = new List<ProbabilityObject>();
        }

        public void CreateProbObjectForList(int id, string name, double probValue)
        {
            ProbabilityObject prob1 = new ProbabilityObject();
            prob1.Id = id;
            prob1.Name = name;
            prob1.ProbValue = probValue;
            probabilityList.Add(prob1);
        }

        public string RandomizeProbabilitySelection(Random r)
        {
            string selectedElement = string.Empty;
            double diceRoll = r.NextDouble();

            double cumulative = 0.0;
            foreach (ProbabilityObject item in probabilityList)
            {
                cumulative += item.ProbValue;
                if (diceRoll < cumulative)
                {
                    selectedElement = item.Name;
                    break;
                }
            }
            return selectedElement;
        }
    }

    public class ProbabilityObject
    {
        private double probValue;
        private int id;
        private string name;

        public double ProbValue
        {
            set
            {
                probValue = value;
            }
            get
            {
                return probValue;
            }
        }

        public int Id
        {
            set
            {
                id = value;
            }
            get
            {
                return id;
            }
        }

        public string Name
        {
            set
            {
                name = value;
            }
            get
            {
                return name;
            }
        }
    }
}
